package com.cloudcc.mobile.adapter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudcc.cloudframe.adapter.CommonAdapter;
import com.cloudcc.cloudframe.adapter.ViewHolder;
import com.cloudcc.cloudframe.model.DynamicModel.Comments;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.util.photo.Constants;
import com.cloudcc.mobile.view.activity.UserInfoActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

public class DynamicCommentAdapter extends CommonAdapter<Comments> {

	private MyOnItemClickListener l;
	
	public DynamicCommentAdapter(Context mContext) {
		super(mContext);
	}

	@Override
	public void convert(ViewHolder vh, Comments t) {

		ImageView userLogo = vh.getView(R.id.chatter_detail_icon);
		TextView tvNickName = vh.getView(R.id.detail_nicheng);
		TextView tvBody = vh.getView(R.id.detail_wbnr);
		TextView tvTime = vh.getView(R.id.timeee);
		TextView tvCountZan = vh.getView(R.id.countzan);
		ImageButton ibMore =vh.getView(R.id.chatter_detailandcom_more);

		if (t.photo) {
			//String url = UrlTools.url + UrlTools.imgurlsee + t.authorId + "&binding="
				//	+ AppContext.binding;
			String url = UrlTools.getTopImage(t.authorId);
			ImageLoader.getInstance().displayImage(StringUtils.replace(url, "https://", "http://"), userLogo,
					Constants.chater_index_icon_optionst);
		}else{
			userLogo.setImageResource(R.drawable.person110);
		}
		tvNickName.setText(t.authorIdname);
		tvTime.setText(t.createDate);
		tvCountZan.setText(t.praisenum);
		tvBody.setText("");
		setBody(t.body, tvBody);
		final int pos  =vh.getPostion();
		ibMore.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (l!=null) {
					Comments c=getItem(pos);
					l.onClickMore(c, pos);
					
				}
			}
		});
		

	}

	private void setBody(String bodyText, TextView tvBody) {
		StringBuilder sb = new StringBuilder();
		int a = 0;
		int b = 0;
		// 这里没有考虑body长度大于100的情况，你自己考虑一下吧
		tvBody.setMovementMethod(LinkMovementMethod.getInstance());
		String pattern = "<a[^>]*>([^<]*)</a>";
		Matcher matcher = Pattern.compile(pattern).matcher(bodyText);
		int lastSpanEndIndex = 0; // 每次匹配成功的字符串的最后索引
		SpannableString spannableStr = null; // "@XXX"
		String aLabelString = null; // a标签，形如"<a href="xxx">fdsafd</a>"
		String Id = null;
		while (matcher.find()) {
			a++;
			int index = matcher.start();

			aLabelString = matcher.group();

			tvBody.append(bodyText.substring(lastSpanEndIndex, index));
			b = index;
			String userIdTemp = aLabelString.substring(aLabelString
					.indexOf("&userid="));
			Id = userIdTemp.substring(userIdTemp.indexOf("=") + 1,
					userIdTemp.indexOf("'"));

			ChatterClickableSpan chatterClickableSpan = new ChatterClickableSpan(
					Id, mContext);

			spannableStr = new SpannableString(Html.fromHtml(aLabelString)
					.toString());

			spannableStr.setSpan(chatterClickableSpan, 0,
					spannableStr.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

			tvBody.append(spannableStr);

			lastSpanEndIndex = matcher.end();
		}
		if (a == 0) {
			tvBody.setText(Html.fromHtml(bodyText));
		} else {
			if (lastSpanEndIndex < bodyText.length()) {
				tvBody.append(Html.fromHtml(bodyText.substring(
						lastSpanEndIndex, bodyText.length())));
			}
		}
	}

	// 人名单击的监听
	public static class ChatterClickableSpan extends ClickableSpan {
		String iId;
		Context context;

		public ChatterClickableSpan(String userId, Context context) {
			super();
			this.iId = userId;
			this.context = context;
		}

		@Override
		public void onClick(View widget) {
			Intent intent = new Intent(context, UserInfoActivity.class);
			intent.putExtra("userId", iId);
			context.startActivity(intent);
		}
	}
	
	
	public void setMyOnItemClickListener(MyOnItemClickListener l){
		this.l =l;
	}
	
	/**
	 * 点击处理
	 * @author seven
	 *
	 */
	public interface MyOnItemClickListener{
		void onClickMore(Comments t,int pos);
	}
	

	@Override
	public int getLayoutId() {
		return R.layout.dynamic_comments_item;
	}

}
